//
//  StrTableViewController.m
//  TeamProject_Travel
//
//  Created by lanou3g on 16/3/25.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "StrDetailTableViewController.h"
#import "StrTableViewController.h"
#import "UIImageView+WebCache.h"
#import "CDHttpHelper.h"
#import "StrModel.h"
@interface StrTableViewController ()
@property(nonatomic,strong)NSMutableArray * allDataArray;
@end
#define url @"https://chanyouji.com/api/wiki/destinations/55.json?page=1"
@implementation StrTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /*
     
     攻略详情的下级攻略界面
     
     */
    self.allDataArray = [NSMutableArray array];
    [self handleData];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}

- (void)handleData{
    [[CDHttpHelper defaultCDHttpHelper] get:[NSString stringWithFormat:@"https://chanyouji.com/api/wiki/destinations/%ld.json?page=1",self.ID] params:nil success:^(id responseObj) {
        NSArray *array = [NSJSONSerialization JSONObjectWithData:responseObj options:NSJSONReadingMutableLeaves error:nil];
        for (int i = 0; i < array.count - 1; i++) {
            [self.allDataArray addObject:array[i]];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.allDataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *array = self.allDataArray[section][@"pages"];
    return array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    NSArray *array = self.allDataArray[indexPath.section][@"pages"];
    cell.textLabel.text = [NSString stringWithFormat:@"   %@",array[indexPath.row][@"title"]];
    
    cell.accessoryType = 1;
    // Configure the cell...
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 60;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(5, 0, [UIScreen mainScreen].bounds.size.width, 60)];
    view.backgroundColor = [UIColor whiteColor];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.frame = CGRectMake(60, 0, 150, 30);
    UILabel *enTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 30, 150, 30)];
    [view addSubview:imageView];
    [view addSubview:titleLabel];
    [view addSubview:enTitleLabel];
    
    if (section == 0) {
        imageView.image = [UIImage imageNamed:@"overview.png"];
        titleLabel.text = @"概览";
        enTitleLabel.text = @"Overview";
    }else if (section == 1){
        imageView.image = [UIImage imageNamed:@"note.png"];
        titleLabel.text = @"出行须知";
        enTitleLabel.text = @"note";
    }else if (section == 2){
        imageView.image = [UIImage imageNamed:@"arrive.png"];
        titleLabel.text = @"路线";
        enTitleLabel.text = @"Arrive";
    }else if (section == 3){
        imageView.image = [UIImage imageNamed:@"traffic.png"];
        titleLabel.text = @"当地交通";
        enTitleLabel.text = @"Traffic";
    }else if (section == 4){
        imageView.image = [UIImage imageNamed:@"hotel.png"];
        titleLabel.text = @"住宿";
        enTitleLabel.text = @"hotel";
    }else if (section == 5){
        imageView.image = [UIImage imageNamed:@"food.png"];
        titleLabel.text = @"美食";
        enTitleLabel.text = @"food";
    }else if (section == 6){
        imageView.image = [UIImage imageNamed:@"shopping.png"];
        titleLabel.text = @"购物";
        enTitleLabel.text = @"shopping";
    }else{
        imageView.image = [UIImage imageNamed:@"huzhao.png"];
        titleLabel.text = @"离境须知";
        enTitleLabel.text = @"Departure";
    }
    
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *array = self.allDataArray[indexPath.section][@"pages"];
    StrDetailTableViewController *strDetailVC = [[StrDetailTableViewController alloc]init];
    strDetailVC.allDataArray = [NSMutableArray array];
    [strDetailVC.allDataArray addObjectsFromArray:array[indexPath.row][@"children"]];
    [self.navigationController pushViewController:strDetailVC animated:YES];
}

@end
